﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OurMind.Core.UnionCollection
{
    internal delegate ICollection GetCollectionActionHandler();
    internal delegate ICollection<T> GetCollectionActionHandler<T>();
    internal delegate int GetCollectionCountActionHandler();
    internal delegate T GetCollectionElementAtFuncHandler<T>(int index);

    internal class UnionCollection<T>
    {
        public virtual int GetCount()
        {
            return 0;
        }

        public virtual T GetElementAt(int index)
        {
            return default(T);
        }
    }

    ///<summary>某种集合。</summary>
    internal class HandledUnionCollection<T> : UnionCollection<T>
    {
        private GetCollectionActionHandler<T> _getCollectionAction;

        public HandledUnionCollection([NotNull] GetCollectionActionHandler<T> getCollectionAction)
        {
            _getCollectionAction = getCollectionAction;
        }

        ///<summary>获得集合。</summary>
        public ICollection<T> GetCollection()
        {
            return _getCollectionAction.Invoke();
        }

        public override int GetCount()
        {
            var coll = _getCollectionAction.Invoke();
            if (coll == null) return 0;
            return coll.Count;
        }

        public override T GetElementAt(int index)
        {
            var coll = _getCollectionAction.Invoke();
            return coll.ElementAt(index);
        }
    }

    internal class UndefineUnionCollection<T> : UnionCollection<T>
    {
        private GetCollectionCountActionHandler _getCollectionCountAction;
        private GetCollectionElementAtFuncHandler<T> _getCollectionElementAtFunc;

        public UndefineUnionCollection(
            [NotNull] GetCollectionCountActionHandler getCountHandler,
            [NotNull] GetCollectionElementAtFuncHandler<T> getObjHandler)
        {
            _getCollectionCountAction = getCountHandler;
            _getCollectionElementAtFunc = getObjHandler;
        }

        public override int GetCount()
        {
            return _getCollectionCountAction();
        }
        public override T GetElementAt(int index)
        {
            return _getCollectionElementAtFunc(index);
        }
    }

   
}
